//
//  BaseUser.swift
//  CFUnion
//
//  Created by 苑伟 on 16/9/18.
//  Copyright © 2016年 苑伟. All rights reserved.
//

import UIKit

open class BaseUser: NSObject {
    
//    var token: String!
//        {
//        didSet{
//            self.saveToken(token: token)
//        }
//    }
    var isLogined:Bool?
//    var id: String?
//    var imageUrl: String?
//    var partyIdName: String?
    
//    var username: String?
//    var loginName: String?
//    var supporterPhone: String?

    override init() {
        //isLogined = false
        super.init()
        
        isLogined = isLogin()
    }
    
    private static let instance : BaseUser = {
        return BaseUser()
    }()
    
    class func shareUser() -> BaseUser {
        return instance
    }
    
//    required init?(coder aDecoder: NSCoder) {
//        isLogined = aDecoder.decodeBool(forKey: "isLogined")
//        id = aDecoder.decodeObject(forKey: "id") as? String
//        imageUrl = aDecoder.decodeObject(forKey: "imageUrl") as? String
//        partyIdName = aDecoder.decodeObject(forKey: "partyIdName") as? String
//        token = aDecoder.decodeObject(forKey: "token") as? String
//        username = aDecoder.decodeObject(forKey: "username") as? String
//        loginName = aDecoder.decodeObject(forKey: "loginName") as? String
//        supporterPhone = aDecoder.decodeObject(forKey: "supporterPhone") as? String
//    }
//    
//    func encodeWithCoder(aCoder: NSCoder) {
//        if isLogined != nil {aCoder.encode(isLogined!, forKey: "isLogined")}
//        if id != nil {aCoder.encode(id!, forKey: "id")}
//        if imageUrl != nil {aCoder.encode(imageUrl, forKey: "imageUrl") }
//        if partyIdName != nil {aCoder.encode(partyIdName, forKey: "partyIdName") }
//        if token != nil {aCoder.encode(token, forKey: "token")}
//        if username != nil {aCoder.encode(username, forKey: "username") }
//        if loginName != nil {aCoder.encode(loginName, forKey: "loginName") }
//        if supporterPhone != nil {aCoder.encode(supporterPhone, forKey: "supporterPhone") }
//    }
//    
//    static let userPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last!.appending("/user.plist")
//    private func createFile(){
//        let fileManager = FileManager.default
//        if fileManager.fileExists(atPath: BaseUser.userPath) == true
//        {
//            do {
//                try fileManager.removeItem(atPath: BaseUser.userPath)
//                fileManager.createFile(atPath: BaseUser.userPath, contents: nil, attributes: nil)
//            } catch {
//                
//            }
//        }
//        else
//        {
//            fileManager.createFile(atPath: BaseUser.userPath, contents: nil, attributes: nil)
//        }
//    }
    
//    private class func loadUser() -> BaseUser {
//        let user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? BaseUser
//        if user != nil {return user! }
//        else {return BaseUser()}
//    }
    
    func logout() {
        isLogined = false
        saveToken(token: "")
    }

    //保存用户密码
    func savePassWord(password: String) {
        UserDefaults.standard.set(password, forKey: KPassWord)
        UserDefaults.standard.synchronize()
    }
    
    //保存用户名和密码
    func saveUserInfo(name: String, password: String) {
        UserDefaults.standard.set(name, forKey: KUserName)
        UserDefaults.standard.set(password, forKey: KPassWord)
        UserDefaults.standard.synchronize()
    }
    //获取用户名
    func getUserName() -> String {
        var result = ""
        if let tempRes = UserDefaults.standard.object(forKey: KUserName) {
            result = tempRes as! String
        }
        return result
    }
    //获取用户密码
    func getPassword() -> String {
        var result = ""
        if let tempRes = UserDefaults.standard.object(forKey: KPassWord) {
            result = tempRes as! String
        }
        return result
    }
    //保存用户token
    func saveToken(token: String) {
        UserDefaults.standard.set(token, forKey: KToken)
        UserDefaults.standard.synchronize()
    }
    //获取用户token
    func getToken() -> String {
        var result = ""
        if let temp = UserDefaults.standard.object(forKey: KToken) {
            result = temp as! String
        }
        return result
    }
    //判断用户登录
    func isLogin() -> Bool {
        var result = false
        if let tmpToken = UserDefaults.standard.object(forKey: KToken) {
            if (tmpToken  as! String).isEmpty == false {
                result = true
            }
        }
        return result
    }
}
